import 'package:alice/extensions/extensions.dart';
import 'package:alice/pages/home/home_page.dart';
import 'package:alice/pages/profile/profile_page.dart';
import 'package:alice/widgets/super_navigation_rail.dart';
import 'package:animations/animations.dart';
import 'package:flutter/material.dart';

class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  late List<Widget> mainTabs;

  int _tabIndex = 0;

  @override
  void initState() {
    super.initState();
    mainTabs = [
      HomePage(),
      ProfilePage(),
    ];
  }

  void changeTabIndex(int index) {
    setState(() => _tabIndex = index);
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        SuperNavigationRail(
          selectedIndex: _tabIndex,
          leading: Padding(
            padding: const EdgeInsets.only(bottom: 20.0, top: 10.0),
            child: Image.asset(
              "assets/images/logo.png",
              width: 40,
              height: 40,
            ),
          ),
          onDestinationSelected: changeTabIndex,
          destinations: [
            NavigationRailDestination(
              icon: Icon(Icons.border_all),
              label: Text('应用'),
            ),
          ],
          trailing: Padding(
            padding: const EdgeInsets.symmetric(vertical: 6.0),
            child: InkWell(
              customBorder: CircleBorder(side: BorderSide(width: 0.0, style: BorderStyle.none)),
              splashColor: context.colorScheme.primary.withOpacity(0.12),
              hoverColor: context.colorScheme.primary.withOpacity(0.04),
              onTap: () {
                print('添加新的');
              },
              child: Icon(
                Icons.add,
                size: 24,
                color: Colors.white,
              ),
            ),
          ),
          bottomDestinations: [
            NavigationRailDestination(
              icon: Icon(Icons.person),
              label: Text('我的'),
            ),
          ],
        ),
        const VerticalDivider(thickness: 1, width: 1),
        Expanded(
          child: PageTransitionSwitcher(
            transitionBuilder:
                (Widget child, Animation<double> primaryAnimation, Animation<double> secondaryAnimation) =>
                    FadeThroughTransition(
              animation: primaryAnimation,
              secondaryAnimation: secondaryAnimation,
              child: child,
            ),
            duration: const Duration(milliseconds: 300),
            child: mainTabs[_tabIndex],
          ),
        ),
      ],
    );
  }
}
